home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-05 / lan.zip / GET.BAK < prev    next >
Text File  |  1991-07-18  |  4KB  |  150 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3.  
  4. #define PORT 0
  5.  
  6. /************************************************************************/
  7. void port_init(port)                  /* Initialize the RS232 port */
  8. int port;
  9. {
  10.  union REGS r;
  11.  
  12.  r.x.dx = port;                                  /* Gets the RS232 port */
  13.  r.h.ah = 0;                                /* Initialize port function */
  14.  r.h.al = 251;                                    /* 19.2kb Baud, 8,N,1 */
  15.  int86 (0x14,&r,&r);
  16. }
  17. /***********************************************************************/
  18. check_stat(port)                /* Checks the status of the RS232 port */
  19. int port;
  20. {
  21.  union REGS r;
  22.  
  23.  r.x.dx = port;                                  /* Gets the RS232 port */
  24.  r.h.ah = 3;                                  /* Send charcter function */
  25.  int86 (0x14,&r,&r);
  26.  return r.x.ax;
  27. }
  28. /************************************************************************/
  29. void sport(port,c)            /* Sends a charcter out to the RS232 port */
  30. int port;                                                   /* I/O port */
  31. char c;                                             /* Charcter to send */
  32. {
  33.  union REGS r;
  34.  
  35.  r.x.dx = port;                                  /* Gets the RS232 port */
  36.  r.h.al = c;                                        /* Charcter to send */
  37.  r.h.ah = 1;                                  /* Send charcter function */
  38.  int86 (0x14,&r,&r);
  39.  if (r.h.ah & 128)                             /* Check the seventh bit */
  40.  {
  41.     printf ("send error detected in the RS232 port %d", r.h.ah);
  42.     exit(1);
  43.  }
  44. }
  45. /************************************************************************/
  46. rport(port)                     /* Reads a charcter from the RS232 port */
  47. int port;
  48. {
  49.  union REGS r;
  50.  
  51.  while (!(check_stat(PORT) &256))
  52.     if (kbhit())                               /* Abort on keypress */
  53.     {
  54.      getch();
  55.      exit(1);
  56.     }
  57.  r.x.dx = port;                                           /* RS232 port */
  58.  r.h.ah = 2;                                 /* Reads charcter function */
  59.  int86(0x14,&r,&r);
  60.  if (r.h.ah & 128)
  61.     printf ("read error detected in RS232 port");
  62.  return r.h.al;
  63. }
  64. /************************************************************************/
  65. void wait(port)                                  /* Wait for a responce */
  66. int port;
  67. {
  68.  if (rport(port) != '.')
  69.  {
  70.   printf ("Communication Error\n");
  71.   exit(1);
  72.  }
  73. }
  74. /************************************************************************/
  75. void send_file_name(f)                        /* Send the file name */
  76. char *f;
  77. {
  78.  do
  79.   sport(PORT,'?');
  80.  while (!kbhit() && !(check_stat(PORT)&256));
  81.   if (kbhit())
  82.    {
  83.     getch();
  84.     exit(1);
  85.    }
  86.  wait(PORT);
  87.  while (*f)
  88.  {
  89.   sport(PORT,*f++);
  90.   wait(PORT);
  91.  }
  92.  sport(PORT,'\0');
  93.  wait (PORT);
  94. }
  95. /************************************************************************/
  96. void rec_file(fname)         /* Recive a file through the sprcified port */
  97. char *fname;
  98. {
  99.  FILE *fp;
  100.  char ch;
  101.  union
  102.  {
  103.   char c[2];
  104.   unsigned int count;
  105.  }
  106.  cnt;
  107.  printf ("Receiving %s\n",fname);
  108.  if (!(fp = fopen(fname,"wb")))
  109.  {
  110.   printf ("cannot open output file %s\n",fname);
  111.   exit(1);
  112.  }
  113.  sport(PORT,'s');                                   /* Get file length */
  114.  wait(PORT);
  115.  
  116.  send_file_name(fname);
  117.  
  118.  sport (PORT,'.');
  119.  cnt.c[0] = rport(PORT);
  120.  sport(PORT,'.');
  121.  cnt.c[1] = rport(PORT);
  122.  sport(PORT,'.');
  123.  
  124.  for (; cnt.count; cnt.count--)
  125.  {
  126.   ch = rport(PORT);
  127.   putc (ch,fp);
  128.   if (ferror(fp))
  129.   {
  130.    printf ("error writing file %s\n",fname);
  131.    exit(1);
  132.   }
  133.   sport (PORT,'.');
  134.  }
  135.  fclose (fp);
  136. }
  137. /*************************************************************************/
  138. main (argc,argv)
  139. int argc;
  140. char *argv[];
  141. {
  142.  if (argc != 2)
  143.  {
  144.   printf ("Usage:  GET <filename.ext>");
  145.   exit(1);
  146.  }
  147.  port_init(PORT);
  148.  rec_file(argv[1]);
  149. }
  150.